package test;
//检测字符串是否为回文
public class Solution {
    public boolean isPalindrome(String s) {
        s=s.toLowerCase();
        int left=0;
        int right=s.length()-1;
        while(left < right) {
            while(!isValid(s.charAt(left)) && left<right) {
                left++;
            }
            while (!isValid(s.charAt(right)) && left<right) {
                right--;
            }
            if(s.charAt(left)==s.charAt(right)) {
                left++;
                right--;
            } else {
                return false;
            }
        }
        return true;
    }

    //方式1：
    public boolean isValid1(char ch) {
        if(ch>='0' && ch<='9' || ch>='a' && ch<='z') {
            return true;
        } else {
            return false;
        }
    }
    //方式2：
    public Boolean isValid(char ch) {
        if (Character.isDigit(ch) || Character.isLetter(ch)) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        Solution solution=new Solution();
        String str="race a car";
        System.out.println(solution.isPalindrome(str));
    }
}